//#include "stdio.h"
//#include "stdlib.h"
//#include "malloc.h"
//#include "string.h"
//#include "queue.c"
#include <iostream>
#include <queue>
using namespace std;

typedef struct
{
    int freeTime; //0 means free
    int busyTimeTotal;
} RunwayType;

typedef struct
{
    int airplaneID;
    int appearTime;
} ElementType;

#define MAXRUNWAYS 100

int main()
{
    RunwayType runways[MAXRUNWAYS];
    queue<ElementType> arrivalQueue, departureQueue;
    int landWaitTimeTotal = 0;
    int takeoffWaitTimeTotal = 0;
    int busyTimeTotal = 0;
    int arrivalTotal = 0;
    int departureTotal = 0;
    int currentTime = 0;
    int landingTime, takeoffTime,  numRunways;
    int i, numArrival, numDeparture;
    int finishFlag = 0, allRunwaysFree = 1;
    ElementType airplane;

    //arrivalQueue = CreateQueue(10 * MAXRUNWAYS);
    //departureQueue = CreateQueue(10 * MAXRUNWAYS);


    printf("Please input number of runways, landing time, takeoff time:\n");
    scanf("%d %d %d", &numRunways, &landingTime, &takeoffTime);
    for ( i = 1; i <= numRunways; i++)
    {
        runways[i].freeTime = 0;
        runways[i].busyTimeTotal = 0;
    }
    printf("Current Time: %4d\n", currentTime);

    for (;;)
    {
        //1 min start
        if (!finishFlag)
        {
            printf("Please input number of arrivals, number of departures required in next minute:\n");
            scanf("%d %d", &numArrival, &numDeparture);
            if ((numArrival < 0) && (numDeparture < 0))
                finishFlag = 1;
        }
        for (i = 0; i < numArrival; i++)
        {
            airplane.airplaneID = ++arrivalTotal + 5000;
            airplane.appearTime = currentTime;
            //Enqueue(airplane, arrivalQueue);
			arrivalQueue.push(airplane);
        }
        for (i = 0; i < numDeparture; i++)
        {
            airplane.airplaneID = ++departureTotal;
            airplane.appearTime = currentTime;
            //______________________;
            //Enqueue(airplane, departureQueue);
			departureQueue.push(airplane);
        }
        for ( i = 1; i <= numRunways; i++)
        {
            if (runways[i].freeTime == 0)
            {
                //if (_____________)
                if (!arrivalQueue.empty())
                {
                    //airplane = _____________;
                    airplane = arrivalQueue.front();
					arrivalQueue.pop();
                    runways[i].freeTime = landingTime;
                    runways[i].busyTimeTotal += landingTime;
                    printf("airplane %04d is ready to land on runway %02d\n", airplane.airplaneID, i);
                    landWaitTimeTotal += currentTime - airplane.appearTime;
                }
                //else if(___________)
                else if ( !departureQueue.empty() )
                {
                    //airplane = _________________;
                    airplane = departureQueue.front();
					departureQueue.front();
                    runways[i].freeTime = takeoffTime;
                    runways[i].busyTimeTotal += takeoffTime;
                    printf("airplane %04d is ready to takeoff on runway %02d\n", airplane.airplaneID, i);
                    takeoffWaitTimeTotal += currentTime - airplane.appearTime;
                }
            }//if (runways[i].freeTime == 0) {
        } //for ( i = 1; i <= numRunways; i++)

        //1 min past
        currentTime++;
        printf("Current Time: %4d\n", currentTime);
        allRunwaysFree = 1;
        for ( i = 1; i <= numRunways; i++)
            if (runways[i].freeTime)
            {
                runways[i].freeTime--;
                if (!runways[i].freeTime)
                    printf("runway %02d is free\n", i);
                else
                    allRunwaysFree = 0;
            }
        /*
        if ( finishFlag && _______________ && _______________ && allRunwaysFree)
        	break;
        */
        //if(finishFlag && IsEmpty(arrivalQueue) && IsEmpty(departureQueue) && allRunwaysFree)
        //  break;
        if(finishFlag && IsEmpty(arrivalQueue) && IsEmpty(departureQueue) && allRunwaysFree)
            break;
    } //for (;;)

    printf("simulation finished\n");
    printf("simulation time: %4d\n",  currentTime);
    float avLandWaitTime =  (float)landWaitTimeTotal/arrivalTotal;
    printf("average waiting time of landing: %4.1f\n", avLandWaitTime);
    float avTakeoffWaitTime =  (float)takeoffWaitTimeTotal/departureTotal;
    printf("average waiting time of takeoff: %4.1f\n", avTakeoffWaitTime);
    for ( i = 1; i <= numRunways; i++)
    {
        busyTimeTotal += runways[i].busyTimeTotal;
        printf("runway %02d busy time: %4d\n",  i, runways[i].busyTimeTotal);
    }
    float busyPercentage = ((float)busyTimeTotal)/numRunways*100/currentTime;
    printf("runway average busy time percentage: %4.1f%%\n",  busyPercentage);
    return 0;
}

